import AsyncStorage from "@react-native-async-storage/async-storage";
import { Alert } from "react-native";
import queryApi from "../api/query";

export const register = (user, callback, setIsLoading) => {
  return async (dispatch) => {
    try {
      console.log("开始注册");
      setIsLoading(true);
      const response = await queryApi.post("/user/signup", user);

      await AsyncStorage.setItem("token", response.data.token);
      await AsyncStorage.setItem("role", response.data.user.role);

      console.log("Token stored");

      dispatch({
        type: "REGISTER",
        payload: {
          token: response.data.token,
          role: response.data.user.role,
        },
      });
      Alert.alert("提示", "注册成功!", [
        {
          text: "确定",
          onPress: () => callback(),
        },
      ]);
    } catch (error) {
      // dispatch({
      //   type: "add_error",
      //   payload: "Something went wrong with signup",
      // });
      Alert.alert("提示", "注册失败!");
      console.log(error);
    }
    setIsLoading(false);
  };
};

export const signin = (account, password, setIsLoading) => {
  return async (dispatch) => {
    try {
      console.log("开始登陆");
      setIsLoading(true);
      const response = await queryApi.post("/user/signin", {
        account,
        password,
      });
      // console.log("-------------", response);
      await AsyncStorage.setItem("token", response.data.token);
      await AsyncStorage.setItem("role", response.data.user.role);
      console.log("Token stored");
      setIsLoading(false);
      dispatch({
        type: "SIGNIN",
        payload: {
          token: response.data.token,
          role: response.data.user.role,
          user: response.data.user,
          isSignIn: true,
        },
      });
    } catch (error) {
      // dispatch({
      //   type: "add_error",
      //   payload: "Something went wrong with signin",
      // });
      setIsLoading(false);
      Alert.alert("提示", "登录失败,请检查账号和密码!");
      console.log("Someting wrong", error.message);
    }
  };
};

export const updateMessage = (user, setIsLoading, setIsEditable) => {
  return async (dispatch) => {
    try {
      console.log("开始修改个人信息");
      setIsLoading(true);
      const token = await AsyncStorage.getItem("token");
      const response = await queryApi.patch("/user/update/" + user._id, user, {
        headers: { Authorization: token },
      });

      // console.log("-----result--------", response.data.data);
      await AsyncStorage.setItem("role", response.data.data.role);

      dispatch({
        type: "UPDATE",
        payload: {
          role: response.data.data.role,
          user: response.data.data,
        },
      });
      Alert.alert("提示", "修改个人信息成功!", [
        {
          text: "确定",
          onPress: () => setIsEditable(false),
        },
      ]);
    } catch (error) {
      // dispatch({
      //   type: "add_error",
      //   payload: "Something went wrong with signin",
      // });
      Alert.alert("提示", "修改个人信息失败");
      console.log("修改个人信息失败", error.message);
    }
    setIsLoading(false);
  };
};

export const signout = () => {
  return async (dispatch) => {
    await AsyncStorage.clear();
    dispatch({ type: "SIGNOUT" });
  };
};
